﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aoxuelf.MDSTool.Model;
namespace Aoxuelf.MDSTool.SQL
{
    public abstract class MethodWriter
    {
        protected MethodWriteInfo methodWriteInfo;
        protected StringBuilder builder;

        protected MethodWriter(MethodWriteInfo methodWriteInfo)
        {
            this.methodWriteInfo = methodWriteInfo;
        }

        public virtual string Write()
        {
            builder = new StringBuilder();
            WriteSummary();
            WriteAuthor();
            WriteSQLParams();
            WriteReturns();            
            WriteRemarks();
            WriteMethod();
            return builder.ToString();
        }

        protected void WriteMethod()
        {
            WriteMethodSig();
            builder.AppendLine("{");
            WriteMethodBody();
            builder.AppendLine("}");
        }

        protected virtual void WriteReturns()
        {
            builder.AppendLine("/// <returns>");
            WriteReturnBody();
            builder.AppendLine("/// </returns>");
        }

        protected virtual void WriteReturnBody()
        { 
            builder.AppendLine("/// <content type=\"DataTable\" name=\"DT\">");
            WriteColumns();
            builder.AppendLine("/// </content>");
        }

        protected abstract void WriteMethodSig();

        protected abstract void WriteMethodBody();

        protected void WriteSummary()
        {
            builder.AppendLine("/// <summary>");
            builder.AppendLine("/// " + methodWriteInfo.Summary ?? string.Empty);
            builder.AppendLine("/// </summary>");
        }

        protected void WriteRemarks()
        {
            builder.AppendLine("/// <remarks>");
            string text = methodWriteInfo.Remarks ?? string.Empty;
            string[] lst = text.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string s in lst)
            {
                builder.AppendLine("/// " + s);
            }
            builder.AppendLine("/// </remarks>");
        }

        protected void WriteAuthor()
        {
            builder.AppendLine(string.Format("/// <author>{0}</author>", methodWriteInfo.Author ?? string.Empty));
        }

        protected void WriteSQLParam(SQLParamInfo sqlParam)
        {
            string format = "/// <param type=\"{0}\" name=\"{1}\">{2}</param>";
            builder.AppendLine(string.Format(format, sqlParam.Type, sqlParam.Name, sqlParam.Text));
        }

        protected virtual void WriteSQLParams()
        {
            foreach (SQLParamInfo sqlParam in methodWriteInfo.SQLParams)
            {
                WriteSQLParam(sqlParam);
            }
        }

        protected void WriteColumns()
        {
            string format = "/// <column name=\"{0}\">{1}</column>";
            foreach (ColumnInfo column in methodWriteInfo.Columns)
            {
                builder.AppendLine(string.Format(format, column.ColumnName, column.ColumnText));
            }
        }

        protected void WriteContent(string name, string text, string type)
        {
            WriteContent(new ReturnContent { Name = name, Text = text, Type = type });
        }

        protected void WriteContent(ReturnContent content)
        {
            string format = "/// <content type=\"{0}\" name=\"{1}\">{2}</content>";
            builder.AppendLine(string.Format(format, content.Type ?? "string", content.Name, content.Text ?? string.Empty));
        }
    }
}
